[section .text]
[bits 64]

extern clock_interrupt_handler
extern get_task_context
extern current

; rdi  <----rsp
; rip
; cs
; rflags
global clock_handler_entry
clock_handler_entry:
    push rdi
    mov rdi, [current]
    cmp rdi, 0
    je .pop_rdi

.store_context:
    push rax
    mov rdi, [current]
    call get_task_context
    mov rdi, rax
    pop rax
    mov [rdi + 8 * 2], rax
    mov [rdi + 8 * 3], rbx
    mov [rdi + 8 * 4], rcx
    mov [rdi + 8 * 5], rdx
    ; 在.store_rdi中回复rdi
    mov [rdi + 8 * 7], rsi
    mov [rdi + 8 * 8], rbp
    ; 在下面回复rsp
    mov [rdi + 8 * 10], r8
    mov [rdi + 8 * 11], r9
    mov [rdi + 8 * 12], r10
    mov [rdi + 8 * 13], r11
    mov [rdi + 8 * 14], r12
    mov [rdi + 8 * 15], r13
    mov [rdi + 8 * 16], r14
    mov [rdi + 8 * 17], r15
.store_rdi:
    mov rcx, rdi
    pop rax
    mov [rcx + 8 * 6], rax

.store_rip:
    mov rax, [rsp + 8 * 0]
    mov [rcx + 8 * 1], rax

.store_cs:
    mov rax, [rsp + 8 * 1]
    mov [rcx + 8 * 18], rax

.store_rflags:
    mov rax, [rsp + 8 * 2]
    mov [rcx + 8 * 0], rax

.store_rsp:
    mov rax, rsp
    add rax, 8 * 3
    mov [rcx + 8 * 9], rax

    jmp .ret

.pop_rdi:
    pop rdi

.ret
    call clock_interrupt_handler
    iretq